//
//  main.m
//  sort-block
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    
//    NSArray *numberArray = @[@"456",@"123",@"1000",@"789"];
//    for(NSString *strNumber in numberArray){
//        NSLog(@"%@",strNumber);
//    }
    
//    NSArray *returnArray = [numberArray sortedArrayUsingSelector:@selector(compare:)];
//    NSLog(@"%@",returnArray);
//    
//    
//    
//    
//    void (^printName)(void) = ^(void){
//        NSLog(@"tony");
//    };
//    
//    printName();
//    
//    NSInteger (^sum)(NSInteger number1 ,NSInteger number2) = ^(NSInteger number1 ,NSInteger number2){
//        return number1 + number2;
//    };
//    NSLog(@"%ld", sum(85,99));
    
    
    
    //定义一个数组
    NSArray *numberArray = @[@"456",@"123",@"1000",@"789",@"123"];

#if 0
    //bloc块的用法
    NSInteger (^sum)(NSInteger a, NSInteger b) = ^NSInteger(NSInteger a ,NSInteger b){
        return a + b;
    };
    
    NSInteger x = sum(5,18);
    NSLog(@"%ld",x);
#endif
 
#if 0
    //block块枚举
    [numberArray enumerateObjectsUsingBlock:^(NSString *object,NSUInteger index,BOOL *stop){
       
        //输出对象和它对应的下标
        NSLog(@"%@,%ld",object,index);

        //停止
        if ([object isEqualToString:@"1000"]){
            *stop = YES;
        }
        
    }];
    
#endif

#if 0
    NSArray *returnArrayByBlock = [numberArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        return [obj1 compare: obj2];
    }];
    NSLog(@"排序后的数组:%@",returnArrayByBlock);
    
#endif
    
    
    
    
    
#if 1
    NSArray *returnArrayByBlock = [numberArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        NSInteger a = [obj1 intValue];
        NSInteger b = [obj2 intValue];
        //比较传入的两个对象的大小,返回值为NSComparisonResult  即升序降序和相等
        if(a < b){
            return NSOrderedAscending;
        } else if (a == b){
            return NSOrderedSame;
        } else {
            return NSOrderedDescending;
        }
    }];
    NSLog(@"排序后的数组:%@",returnArrayByBlock);
    
#endif
    
    
    
    
    return 0;
}
